-module(tr_acceptor).
-behaviour(gen_server).

-export([start_link/1]).

-export([init/1,
         handle_info/2,
         terminate/2]).

-record(state, {socket, request_count=0}).

start_link(Socket) ->
    gen_server:start_link(?MODULE, [Socket], []).

init([Socket]) ->
    {ok, #state{socket=Socket,
                request_count=0}}.

handle_info({tcp, _Port, RawData}, #state{socket=Socket, request_count=Count}) ->
    io:format("~p: ~p~n", [Count,RawData]),
    gen_tcp:send(Socket, RawData),
    inet:setopts(Socket, [{active, once}]),
    {noreply, #state{socket=Socket, request_count=Count+1}}.

terminate(_Reason, _State) ->
    ok.

